package com.openthinks.joyniproject.utils;

 import java.security.MessageDigest;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
   
 /** 
  * 基础加密组件 
  *  
  */  
 public abstract class Encrypter {  
     public static final String KEY_SHA = "SHA";  
     public static final String KEY_MD5 = "MD5";  
   
     /** 
      * MAC 算法可选以下多种算法 
      *  
      * <pre> 
      * HmacMD5  
      * HmacSHA1  
      * HmacSHA256  
      * HmacSHA384  
      * HmacSHA512 
      * </pre> 
      */  
     public static final String KEY_MAC = "HmacMD5";  
   
     /** 
      * BASE64 解密 
      *  
      * @param key 
      * @return 
      * @throws Exception 
      */  
     public static byte[] decryptBASE64(String key) throws Exception {  
         return (new BASE64Decoder()).decodeBuffer(key);  
     }  
   
     /** 
      * BASE64 加密 
      *  
      * @param key 
      * @return 
      * @throws Exception 
      */  
     public static String encryptBASE64(byte[] key) throws Exception {  
         return (new BASE64Encoder()).encodeBuffer(key);  
     }  
   
     /** 
      * MD5 加密 
      *  
      * @param data 
      * @return 
      * @throws Exception 
      */  
     public static byte[] encryptMD5(byte[] data) throws Exception {  
   
         MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);  
         md5.update(data);  
   
         return md5.digest();  
   
     }  
   
     /** 
      * SHA 加密 
      *  
      * @param data 
      * @return 
      * @throws Exception 
      */  
     public static byte[] encryptSHA(byte[] data) throws Exception {  
   
         MessageDigest sha = MessageDigest.getInstance(KEY_SHA);  
         sha.update(data);  
   
         return sha.digest();  
   
     }  
   
     /** 
      * 初始化HMAC密钥 
      *  
      * @return 
      * @throws Exception 
      */  
     public static String initMacKey() throws Exception {  
         KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);  
   
         SecretKey secretKey = keyGenerator.generateKey();  
         return encryptBASE64(secretKey.getEncoded());  
     }  
   
     /** 
      * HMAC 加密 
      *  
      * @param data 
      * @param key 
      * @return 
      * @throws Exception 
      */  
     public static byte[] encryptHMAC(byte[] data, String key) throws Exception {  
   
         SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);  
         Mac mac = Mac.getInstance(secretKey.getAlgorithm());  
         mac.init(secretKey);  
   
         return mac.doFinal(data);  
   
     }
     /**
      * md5加密方法
      * @param plainText
      * @return
      */
     public static String md5s(String str) {
         String md5Str = "";
         try {
             MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
             md5.update(str.getBytes());
             byte b[] = md5.digest();
             StringBuffer buf = new StringBuffer("");
             for (int i : b) {
                 if (i < 0)
                     i += 256;
                 if (i < 16)
                     buf.append("0");
                 buf.append(Integer.toHexString(i));
             }
             md5Str = buf.toString();// 32位的加密
         } catch (Exception e) {
             md5Str = "";
             e.printStackTrace();
         }
         return md5Str;
    }
     
     public static void main(String[] args){
    	 System.out.println(md5s("304868"));
     }
 }